home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Toolkit / Songbird 0.1 / Songbird_0_1_0.exe / components / sbIPlaylistReaderRDF.js < prev    next >
Text File  |  2006-02-08  |  11KB  |  376 lines

  1. /*
  2.  //
  3. // BEGIN SONGBIRD GPL
  4. // 
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright⌐ 2006 Pioneers of the Inevitable LLC
  8. // http://songbirdnest.com
  9. // 
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the ôGPLö).
  12. // 
  13. // Software distributed under the License is distributed 
  14. // on an ôAS ISö basis, WITHOUT WARRANTY OF ANY KIND, either 
  15. // express or implied. See the GPL for the specific language 
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this 
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc., 
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. // 
  23. // END SONGBIRD GPL
  24. //
  25.  */
  26.  
  27. //
  28. // sbIPlaylistReader Object (RSS)
  29. //
  30.  
  31. const SONGBIRD_PLAYLISTRDF_CONTRACTID = "@songbird.org/Songbird/Playlist/Reader/RDF;1";
  32. const SONGBIRD_PLAYLISTRDF_CLASSNAME = "Songbird RDF Playlist Interface";
  33. const SONGBIRD_PLAYLISTRDF_CID = Components.ID("{5778E746-3435-4f7e-9201-99898E9A916A}");
  34. const SONGBIRD_PLAYLISTRDF_IID = Components.interfaces.sbIPlaylistReader;
  35.  
  36. function CPlaylistRDF()
  37. {
  38.   jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
  39.   jsLoader.loadSubScript( "chrome://rmp_demo/content/songbird_interfaces.js", this );
  40. }
  41.  
  42. /* I actually need a constructor in this case. */
  43. CPlaylistRDF.prototype.constructor = CPlaylistRDF;
  44.  
  45. /* the CPlaylistRDF class def */
  46. CPlaylistRDF.prototype = 
  47. {
  48.   originalURL: "",
  49.   
  50.   m_document: null,
  51.   m_playlistmgr: null,
  52.   m_playlist: null,
  53.   m_library: null,
  54.   m_query: null,
  55.   
  56.   m_guid: "",
  57.   m_table: "",
  58.   m_append: false,
  59.   
  60.   Read: function( strURL, strGUID, strDestTable, bAppendOrReplace, /* out */ errorCode )
  61.   {
  62.     try
  63.     {
  64.       errorCode.value = 0;
  65.  
  66.       var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser);
  67.       var pFileReader = (Components.classes["@mozilla.org/network/file-input-stream;1"]).createInstance(Components.interfaces.nsIFileInputStream);
  68.       var pFile = (Components.classes["@mozilla.org/file/local;1"]).createInstance(Components.interfaces.nsILocalFile);
  69.       var pURI = (Components.classes["@mozilla.org/network/simple-uri;1"]).createInstance(Components.interfaces.nsIURI);
  70.  
  71.       if ( domParser && pFile && pURI && pFileReader )
  72.       {
  73.         pURI.spec = strURL;
  74.         if ( pURI.scheme == "file" )
  75.         {
  76.           var cstrPathToPLS = pURI.path;
  77.           cstrPathToPLS = cstrPathToPLS.substr( 3, cstrPathToPLS.length );
  78.           pFile.initWithPath(cstrPathToPLS);
  79.           if ( pFile.isFile() )
  80.           {
  81.             pFileReader.init( pFile, /* PR_RDONLY */ 1, 0, /*nsIFileInputStream::CLOSE_ON_EOF*/ 0 );
  82.             var document = domParser.parseFromStream(pFileReader, null, pFileReader.available(), "application/xml");
  83.             pFileReader.close();
  84.             
  85.             if(document)
  86.             {
  87.               const MediaLibrary = new Components.Constructor("@songbird.org/Songbird/MediaLibrary;1", "sbIMediaLibrary");
  88.               const PlaylistManager = new Components.Constructor("@songbird.org/Songbird/PlaylistManager;1", "sbIPlaylistManager");
  89.  
  90.               this.m_guid = strGUID;
  91.               this.m_table = strDestTable;
  92.               this.m_append = bAppendOrReplace;
  93.               
  94.               this.m_query = new this.sbIDatabaseQuery();
  95.               this.m_query.SetAsyncQuery(true);
  96.               this.m_query.SetDatabaseGUID(strGUID);
  97.  
  98.               this.m_document = document;
  99.               this.m_library = new MediaLibrary();
  100.               this.m_playlistmgr = new PlaylistManager();
  101.               
  102.               this.m_library.SetQueryObject(this.m_query);
  103.               var playlist = this.m_playlistmgr.GetPlaylist(strDestTable, this.m_query);
  104.               
  105.               if(playlist)
  106.               {
  107.                 this.m_playlist = playlist;
  108.                 return this.ProcessXMLDocument(this.m_document);
  109.               }
  110.             }
  111.           }
  112.         }
  113.       }
  114.     }
  115.     catch ( err )
  116.     {
  117.       throw "CPlaylistRDF::Read - " + err;
  118.     }
  119.     
  120.     return false;
  121.   },
  122.   
  123.   Vote: function( strURL )
  124.   {
  125.     try
  126.     {
  127.       return 10000;
  128.     }
  129.     catch ( err )
  130.     {
  131.       throw "CPlaylistRDF::Vote - " + err;
  132.     }
  133.   },
  134.   
  135.   Name: function()
  136.   {
  137.     try
  138.     {
  139.       return "RDF Playlist"; // ????
  140.     }
  141.     catch ( err )
  142.     {
  143.       throw "CPlaylistRDF::Name - " + err;
  144.     }
  145.   },
  146.   
  147.   Description: function()
  148.   {
  149.     try
  150.     {
  151.       return "RDF Playlist"; // ????
  152.     }
  153.     catch ( err )
  154.     {
  155.       throw "CPlaylistRDF::Description - " + err;
  156.     }
  157.   },
  158.   
  159.   SupportedMIMETypes: function( /* out */ nMIMECount )
  160.   {
  161.     var retval = new Array;
  162.     nMIMECount.value = 0;
  163.     
  164.     try
  165.     {
  166.       retval.push( "text/xml" );
  167.       retval.push( "text/rdf+xml" );
  168.       retval.push( "application/xml" );
  169.       nMIMECount.value = retval.length;
  170.     }
  171.     catch ( err )
  172.     {
  173.       throw "CPlaylistRDF::SupportedMIMETypes - " + err;
  174.     }
  175.     return retval;
  176.   },
  177.   
  178.   SupportedFileExtensions: function( /* out */ nExtCount )
  179.   {
  180.     var retval = new Array;
  181.     nExtCount.value = 0;
  182.     
  183.     try
  184.     {
  185.       retval.push( "rdf" );
  186.       retval.push( "xml" );
  187.       
  188.       nExtCount.value = retval.length;
  189.     }
  190.     catch ( err )
  191.     {
  192.       throw "CPlaylistRDF::SupportedFileExtensions - " + err;
  193.     }
  194.     
  195.     return retval;
  196.   },
  197.   
  198.   ProcessXMLDocument: function( xmlDocument )
  199.   {
  200.     var ret = false;
  201.     try
  202.     {
  203.       var document = xmlDocument.documentElement;
  204.       dump("CPlaylistRDF::ProcessXMLDocument - Tag Name: " + document.tagName + "\n");
  205.       
  206.       if(document.tagName == "rdf:RDF")
  207.       {
  208.         dump("CPlaylistRDF::ProcessXMLDocument - RSS/RDF Version: 1.0\n");
  209.         ret = this.ProcessRDFFeed(document);
  210.       }
  211.     }      
  212.     catch(err)
  213.     {
  214.       throw "CPlaylistRDF::ProcessXMLDocument - " + err;
  215.     }
  216.     
  217.     return ret;
  218.   },
  219.   
  220.   ProcessRDFFeed: function( xmlDocument )
  221.   {
  222.     try
  223.     {
  224.       
  225.     }
  226.     catch(err)
  227.     {
  228.       throw "CPlaylistRDF::ProcessXMLDocument - " + err;
  229.     }
  230.     
  231.     return false;
  232.   },
  233.   
  234.   IsMediaUrl: function( the_url )
  235.   {
  236.     if ( ( the_url.indexOf ) && 
  237.           (
  238.             // Protocols at the beginning
  239.             ( the_url.indexOf( "mms:" ) == 0 ) || 
  240.             ( the_url.indexOf( "rtsp:" ) == 0 ) || 
  241.             // File extensions at the end
  242.             ( the_url.indexOf( ".pls" ) != -1 ) || 
  243.             ( the_url.indexOf( ".m3u" ) == ( the_url.length - 4 ) ) || 
  244. //            ( the_url.indexOf( ".rm" ) == ( the_url.length - 3 ) ) || 
  245. //            ( the_url.indexOf( ".ram" ) == ( the_url.length - 4 ) ) || 
  246. //            ( the_url.indexOf( ".smil" ) == ( the_url.length - 5 ) ) || 
  247.             ( the_url.indexOf( ".mp3" ) == ( the_url.length - 4 ) ) ||
  248.             ( the_url.indexOf( ".m4a" ) == ( the_url.length - 4 ) ) ||
  249.             ( the_url.indexOf( ".ogg" ) == ( the_url.length - 4 ) ) ||
  250.             ( the_url.indexOf( ".wma" ) == ( the_url.length - 4 ) ) ||
  251.             ( the_url.indexOf( ".wmv" ) == ( the_url.length - 4 ) ) ||
  252.             ( the_url.indexOf( ".asx" ) == ( the_url.length - 4 ) ) ||
  253.             ( the_url.indexOf( ".asf" ) == ( the_url.length - 4 ) ) ||
  254.             ( the_url.indexOf( ".avi" ) == ( the_url.length - 4 ) ) ||
  255.             ( the_url.indexOf( ".mov" ) == ( the_url.length - 4 ) ) ||
  256.             ( the_url.indexOf( ".mp4" ) == ( the_url.length - 4 ) )
  257.           )
  258.         )
  259.     {
  260.       return true;
  261.     }
  262.     return false;
  263.   },
  264.   
  265.   ConvertUrlToDisplayName: function( url )
  266.   {
  267.     // Set the title display  
  268.     var the_value = "";
  269.     if ( url.lastIndexOf('/') != -1 )
  270.     {
  271.       the_value = url.substring( url.lastIndexOf('/') + 1, url.length );
  272.     }
  273.     else if ( url.lastIndexOf('\\') != -1 )
  274.     {
  275.       the_value = url.substring( url.lastIndexOf('\\') + 1, url.length );
  276.     }
  277.     else
  278.     {
  279.       the_value = url;
  280.     }
  281.     // Convert any %XX to space
  282.     var percent = the_value.indexOf('%');
  283.     if ( percent != -1 )
  284.     {
  285.       var remainder = the_value;
  286.       the_value = "";
  287.       while ( percent != -1 )
  288.       {
  289.         the_value += remainder.substring( 0, percent );
  290.         remainder = remainder.substring( percent + 3, url.length );
  291.         percent = remainder.indexOf('%');
  292.         the_value += " ";
  293.         if ( percent == -1 )
  294.         {
  295.           the_value += remainder;
  296.         }
  297.       }
  298.     }
  299.     if ( the_value.length == 0 )
  300.     {
  301.       the_value = url;
  302.     }
  303.     return the_value;
  304.   },
  305.  
  306.   QueryInterface: function(iid)
  307.   {
  308.       if (!iid.equals(Components.interfaces.nsISupports) &&
  309.           !iid.equals(SONGBIRD_PLAYLISTRDF_IID))
  310.           throw Components.results.NS_ERROR_NO_INTERFACE;
  311.       return this;
  312.   }
  313. }; //CPlaylistRDF
  314.  
  315. /**
  316.  * \class sbPlaylistRDFModule
  317.  * \brief 
  318.  */
  319. var sbPlaylistRDFModule = 
  320. {
  321.   registerSelf: function(compMgr, fileSpec, location, type)
  322.   {
  323.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  324.     compMgr.registerFactoryLocation(SONGBIRD_PLAYLISTRDF_CID, 
  325.                                     SONGBIRD_PLAYLISTRDF_CLASSNAME, 
  326.                                     SONGBIRD_PLAYLISTRDF_CONTRACTID, 
  327.                                     fileSpec, 
  328.                                     location,
  329.                                     type);
  330.   },
  331.  
  332.   getClassObject: function(compMgr, cid, iid) 
  333.   {
  334.     if(!cid.equals(SONGBIRD_PLAYLISTRDF_CID))
  335.         throw Components.results.NS_ERROR_NO_INTERFACE;
  336.  
  337.     if(!iid.equals(Components.interfaces.nsIFactory))
  338.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  339.  
  340.     return sbPlaylistRDFFactory;
  341.   },
  342.  
  343.   canUnload: function(compMgr)
  344.   { 
  345.     return true; 
  346.   }
  347. }; //sbPlaylistRDFModule
  348.  
  349. /**
  350.  * \class sbPlaylistRDFFactory
  351.  * \brief 
  352.  */
  353. var sbPlaylistRDFFactory =
  354. {
  355.   createInstance: function(outer, iid)
  356.   {
  357.     if (outer != null)
  358.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  359.  
  360.     if (!iid.equals(SONGBIRD_PLAYLISTRDF_IID) &&
  361.         !iid.equals(Components.interfaces.nsISupports))
  362.         throw Components.results.NS_ERROR_INVALID_ARG;
  363.  
  364.     return (new CPlaylistRDF()).QueryInterface(iid);
  365.   }
  366. }; //sbPlaylistRDFFactory
  367.  
  368. /**
  369.  * \function NSGetModule
  370.  * \brief 
  371.  */
  372. function NSGetModule(comMgr, fileSpec)
  373.   return sbPlaylistRDFModule;
  374. } //NSGetModule
  375.